import numpy as np, scipy.special as sp, scipy.integrate as ig, copy, re, math
import sys
from Model import Model,Actor,Evidence,TrackSegment,Node,TimeNode,Time,Distributions

class Approach:
    """ Actor1 approaches actor2 """
    def __init__(self):
        self.model = Model()
        self.model.name = "Approach"
        self.model.subscriptions = [""]
        self.model.topNode = "approachmain"
        self.model.independent = True

        act1 = Actor()
        act1.name = "dummy1"
        act1.atype = Actor.NULL
        act2 = Actor()
        act2.name = "dummy2"
        act2.atype = Actor.NULL
        
        tempNode = Node()
        tempNode.getID()
        tempNode.name = "approachmain"
        tempNode.subjects = [act1]
        tempNode.objects = [act2]
        tempNode.evvecs = []
        tempNode.bound = False

        self.model.topNode = tempNode.name
        self.model.nodes.append(copy.deepcopy(tempNode))

        tempNode.name = "approachsub"
        tempEv1 = TrackSegment()
        tempEv1.actor = act1
        tempEv1.segtype = Evidence.MOVE_SLOW
        tempEv1.willBind = True
        tempEv1.endsNode = True
        tempEv1.bound = False
        
        tempEv2 = TrackSegment()
        tempEv2.actor = act2
        tempEv2.segtype = Evidence.POS_STABLE
        tempEv2.willBind = True
        tempEv2.endsNode = True
        tempEv2.bound = False
        
        tempNode.evvecs = [copy.deepcopy(tempEv1)]
        tempNode.evvecs.append(copy.deepcopy(tempEv2))
        
#        # TODO: get interval from somewhere!!!
#        interval = (2000,4564) 
        
        def approachJoint(evvecs):
            track1 = evvecs[0]
            track2 = evvecs[1]
            dist = track1.calculate_distance_to_stationary(track2)
            mean = sum(dist) / len(dist)
            mweight = Approach.meanDensity(-mean)
            print "calculated mean: ", -mean
            print "mweight: ", mweight
            std = np.std(dist)
            stdweight = Approach.stdDensity(std)
            print "calculated std: ", std
            print "stdweight: ", stdweight
            weight = mweight + stdweight
            
            return weight,0.5
        
        tempNode.lfunction = approachJoint
        self.model.nodes.append(copy.deepcopy(tempNode))


    @staticmethod
    def meanDensity(mean):
        weight = Distributions.PDFs.nakagami(mean, 1.5, 20) # TODO: find density function that fits 
        return weight
    
    @staticmethod
    def stdDensity(std):
        weight = Distributions.PDFs.foldednorm(std, 0.5, 1.5)
        return weight
    

